﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using DNA.IO.Compress;
using System.Globalization;

namespace DNA.Mvc.Exporting
{
    public class Packer
    {
        private ExporterContainer Container;

        private IExporterFactory factory;

        public HttpContextBase HttpContext { get; set; }

        public Packer(HttpContextBase httpContext) : this(new ExporterContainer(),httpContext) { }

        public Packer(ExporterContainer container,HttpContextBase httpContext)
        {
            Container = container;
            factory = new DefaultExporterFactory(Container);
            this.HttpContext = httpContext;
        }

        //////Export File
        //public void Do(string temporary, string source, string dest, Package package)
        //{
        //    Container.Exporerts.Clear();
        //    factory.AddDirectory("Theme");
        //    factory.AddPackage();

        //    Container.Process(temporary,
        //        new
        //        {
        //            Theme = new
        //            {
        //                Source = source,
        //                Dest = dest
        //            },
        //            Package = package
        //        },
        //    HttpContext);
        //}

        //Export web
        public void Do(string temporary, Web web, Package package)
        {
            Container.Exporerts.Clear();

            factory.AddWebTemplate();
            factory.AddPackage();

            Container.Process(temporary, new
            {
                Web = web,
                Package = package
            }, HttpContext);
        }

        //Export widget
        public void Do(string temporary, string descriptorID, Package package)
        {
            Container.Exporerts.Clear();
            factory.AddDirectory("Widget");
            factory.AddPackage();

            Container.Process(temporary,
                new
                {
                    Widget ="~/content/widgets/" + descriptorID,
                    Package = package
                },
            HttpContext);
        }

        public void Zip(Stream stream, string temporary, Web web, Package package)
        {
            Do(temporary, web, package);
            Compress(stream, temporary);
        }

        //public void Zip(Stream stream, string temporary, string source, string dest, Package package)
        //{
        //    Do(temporary, source, dest, package);
        //    Compress(stream, temporary);
        //}

        public void Zip(Stream stream, string temporary, string descriptorID, Package package)
        {
            Do(temporary, descriptorID, package);
            Compress(stream, temporary);
        }

        public void Zip(string filename, string temporary, Web web, Package package)
        {
            Zip(File.OpenWrite(filename), temporary, web, package);
        }

        //public void Zip(string filename, string temporary, string source, string dest, Package package)
        //{
        //    Zip(File.OpenWrite(filename), temporary, source, dest, package);
        //}

        public void Zip(string filename, string temporary, string descriptorID, Package package)
        { 
            var file=File.OpenWrite(filename);
            Zip(file, temporary, descriptorID, package);
        }

        internal static void Compress(Stream stream, string temporary)
        {
            var zip = new ZipCompress(stream,temporary);
            zip.AddPath(temporary);
            zip.Compress();
        }
    }
}
